package action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import model.User;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import service.ModuleService;
import action.base.BaseAction;

@Controller
@Scope("prototype")
public class IndexAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
	private ModuleService moduleService;
	private Boolean success = false;

	private List<HashMap<String, Object>> list;
	private HashMap<String, Object> map = new HashMap<String, Object>();
	private File file;
	// 获取上传文件的名称
	private String fileFileName;
	private InputStream stream;

	/**
	 * 风琴结构的 一级菜单
	 * 
	 * @return
	 */
	public String queryMenu() {
		setList(moduleService.queryMenu());
		return "list";
	}

	public String queryUserMenu() {
		User user = super.getUser();
		setList(moduleService.queryUserMenu(user));
		return "list";
	}

	/**
	 * 一级菜单 的 树形结构
	 * 
	 * @return
	 */
	public String queryMenuTree() {
		Integer id = super.getParameterInt("node");
		setList(moduleService.queryMenuTree(id));
		return "list";
	}

	public String queryUserMenuTree() {
		Integer id = super.getParameterInt("node");
		User user = super.getUser();
		map.put("id", id);
		map.put("userid", user.getId());
		setList(moduleService.queryUserMenuTree(map));
		return "list";
	}

	public String down() throws Exception {
		String attach = super.getParameterStr("attach");
		String fname = super.getParameterStr("fileName");
		if (!StringUtils.isNotBlank(attach)) {
			attach = null;
		} else {
			byte[] _attach = attach.getBytes("ISO-8859-1");
			attach = new String(_attach, "UTF-8");
		}
//		if (!StringUtils.isNotBlank(fname)) {
//			fname = null;
//		} else {
//			byte[] _fname = fname.getBytes("ISO-8859-1");
//			fname = new String(_fname, "UTF-8");
//		}
		if (attach != null) {
			String path = "";
			if (attach.substring(1, 2).equals(":")) {
				path = attach;
			} else {
				HttpServletRequest request = ServletActionContext.getRequest();
				String rootpath = request.getSession().getServletContext()
						.getRealPath("/").replace("\\", "/");
				path = rootpath + attach.substring(1);
			}
			File f = new File(path);
			if (f.exists()) {
				setStream(new FileInputStream(f));

			} else
				return "noFile";
			// fileFileName = attach.substring(attach.lastIndexOf("/") + 1);
		} else
			return "noFile";
		fileFileName = fname;
		success = true;
		return "down";
	}

	public Boolean getSuccess() {
		return success;
	}

	public void setSuccess(Boolean success) {
		this.success = success;
	}

	public List<HashMap<String, Object>> getList() {
		return list;
	}

	public void setList(List<HashMap<String, Object>> list) {
		this.list = list;
	}

	public HashMap<String, Object> getMap() {
		return map;
	}

	public void setMap(HashMap<String, Object> map) {
		this.map = map;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public InputStream getStream() {
		return stream;
	}

	public void setStream(InputStream stream) {
		this.stream = stream;
	}

}
